package sdk.pay.utils;

import android.content.Context;
import android.view.Gravity;
import android.widget.Toast;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by Steven on 2016/11/30 0030.
 */

public final class PayToastUtil {
    private static Toast mToast;
    private static Toast mCustomToast;

    public static void showMessage(Context context, String content) {
        showInfo(context, content, Toast.LENGTH_SHORT);

    }

    public static void ShowMessageLong(Context context, String content) {
        showInfo(context, content, Toast.LENGTH_LONG);
    }

    private static void showInfo(Context context, String content, int type) {
        if (null == mToast) {
            mToast = Toast.makeText(context, "", type);
        }
        mToast.setDuration(type);
        mToast.setGravity(Gravity.CENTER, 0, 0);
        mToast.setText(content);
        mToast.show();
    }

    public static void showCustomInfo(Context context, String content, int time) {
        if (null == mCustomToast) {
            mCustomToast = Toast.makeText(context, "", Toast.LENGTH_LONG);
        }
        mCustomToast.setGravity(Gravity.CENTER, 0, 0);
        mCustomToast.setText(content);
        final Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                mCustomToast.show();
            }
        }, 0, time >> 2);

        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                timer.cancel();
                mCustomToast.cancel();
            }
        }, time);
    }
}
